package com.fiume.travel.web;

import com.fiume.travel.enums.StatusEnum;
import com.fiume.travel.exception.ProjectException;
import com.fiume.travel.res.ResponseWrap;
import com.fiume.travel.service.RouteService;
import com.fiume.travel.utils.ExceptionsUtil;
import com.fiume.travel.vo.RouteVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * Description:
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-23 15:52
 * @see com.fiume.travel.web
 */
@RestController
@RequestMapping("route")
@Log4j2
@CrossOrigin
@Api(tags = "线路模块")
public class RouteController {

    @Autowired
    RouteService routeService;

    @Value("${upLoad.webSite}")
    String webSite;

    /**
     * 添加路线
     * @param routeVo 路径请求参数
     * @return 影响行数
     */
    @PostMapping
    public ResponseWrap<Integer> addRoute(@RequestBody RouteVo routeVo) throws ProjectException {
        try {
            Integer flag = routeService.addRoute(routeVo);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("添加线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ADD_ROUTE_FAIL.getCode(),
                    StatusEnum.ADD_ROUTE_FAIL.getMsg());
        }

    }

    /**
     * 编辑线路
     * @param routeVo 线路请求参数
     * @return 影响行数
     */
    @PutMapping
    public ResponseWrap<Integer> updateRoute(@RequestBody RouteVo routeVo) throws ProjectException {
        try {
            Integer flag = routeService.updateRoute(routeVo);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("修改线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.UPDATE_ROUTE_FAIL.getCode(),
                    StatusEnum.UPDATE_ROUTE_FAIL.getMsg());
        }

    }

    /**
     * 查询线路
     * @param routeId 路径id
     * @return 线路详情
     */
    @GetMapping("/{routeId}")
    public ResponseWrap<RouteVo> findRouteById(@PathVariable("routeId") Long routeId) throws ProjectException {
        try {
            RouteVo routeVo = RouteVo.builder().id(routeId).build();
            RouteVo routeResult = routeService.findRouteById(routeVo);
            return ResponseWrap.<RouteVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(routeResult)
                    .build();
        }catch (Exception e){
            log.error("查询线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_ROUTE_ID_FAIL.getCode(),
                    StatusEnum.FIND_ROUTE_ID_FAIL.getMsg());
        }

    }

    /**
     * 分页查询线路
     * @param routeVo 查询条件
     * @param pageNum 分页
     * @param pageSize 每页返回数
     * @return com.github.pagehelper.PageInfo<com.itheima.travel.req.RouteVo>
     */
    @PostMapping("{pageNum}/{pageSize}")
    public ResponseWrap<PageInfo<RouteVo>> findRouteByPage(@RequestBody RouteVo routeVo,
                                                           @PathVariable("pageNum") Integer pageNum,
                                                           @PathVariable("pageSize") Integer pageSize) throws ProjectException {
        try {
            PageInfo<RouteVo> routeByPage = routeService.findRouteByPage(routeVo, pageNum, pageSize);
            return ResponseWrap.<PageInfo<RouteVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(routeByPage)
                    .build();
        }catch (Exception e){
            log.error("分页查询线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_ROUTE_PAGE_FAIL.getCode(),
                    StatusEnum.FIND_ROUTE_PAGE_FAIL.getMsg());
        }

    }

}
